package com.mobzilla.service;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.mobzilla.model.Bug;
import com.mobzilla.model.BugHolder;
import com.mobzilla.model.Priority;
import com.mobzilla.model.Severity;
import com.mobzilla.model.Status;
import com.mobzilla.util.Constants;
import com.mobzilla.util.Lock;

public class SearchServiceImpl implements SearchService {

	private static final String URL_PREFIX = Constants.BASE_URL + "/bug";

	@Override
	public Bug fetchDefectDetails(int bugId) {

		/*
		 * http://172.19.194.33:3000/bug?id=1&include_fields=_all
		 */
		StringBuilder builder = new StringBuilder(URL_PREFIX);
		builder.append("?id=").append(bugId).append("&include_fields=_all");

		String url = builder.toString();

		Lock<String> lock = new Lock<String>();
		HTTPRequestAsync async = new HTTPRequestAsync(url, lock);
		async.execute();

		while (!lock.isDataSet()) {

		}

		if (lock.getData() == null || lock.getData().equals("")) {
			return null;
		}

		Gson gson = new Gson();
		BugHolder responseBean = gson.fromJson(lock.getData(), BugHolder.class);

		if ((null != responseBean) && (responseBean.getBugs().size() == 0)) {
			return null;
		}

		return responseBean.getBugs().get(0);
	}

	@Override
	public List<Bug> fetchAllDefectsWithDetails() {

		StringBuilder builder = new StringBuilder(URL_PREFIX);
		builder.append("?").append("include_fields=_all");

		String url = builder.toString();

		return processForBeanList(url);
	}

	@Override
	public List<Bug> fetchDefectsWithIds(int[] intArray) {
		StringBuilder builder = new StringBuilder(URL_PREFIX);
		builder.append("?id=");
		for (int tempInt : intArray) {
			if (tempInt != 0) {
				builder.append(tempInt + ",");
			}
		}
		builder.deleteCharAt(builder.length() - 1);
		builder.append("&include_fields=_all");

		return processForBeanList(builder.toString());
	}

	@Override
	public List<Bug> fetchDefectsForUser(String email) {
		if (null != email) {
			StringBuilder builder = new StringBuilder(URL_PREFIX);
			builder.append("?assigned_to=").append(email);
			builder.append("&include_fields=_all");

			return processForBeanList(builder.toString());
		}
		return new ArrayList<Bug>();
	}

	@Override
	public List<Bug> fetchDefectsForPriority(Priority priority) {

		StringBuilder builder = new StringBuilder(URL_PREFIX);
		builder.append("?priority=").append(priority);
		builder.append("&include_fields=_all");

		return processForBeanList(builder.toString());
	}

	@Override
	public List<Bug> fetchDefectsForSeverity(Severity severity) {

		StringBuilder builder = new StringBuilder(URL_PREFIX);
		builder.append("?severity=").append(severity);
		builder.append("&include_fields=_all");

		return processForBeanList(builder.toString());
	}
	
	@Override
	public List<Bug> fetchAllShowStoppers() {

		StringBuilder builder = new StringBuilder(URL_PREFIX);
		builder.append("?severity=").append(Severity.BLOCKER);
		builder.append("&priority=").append(Priority.P1);
		builder.append("&include_fields=_all");

		return processForBeanList(builder.toString());
	}
	
	@Override
	public java.util.List<Bug> fetchResolvedDefects() {
		StringBuilder builder = new StringBuilder(URL_PREFIX);
		builder.append("?status=").append(Status.RESOLVED);
		builder.append("&include_fields=_all");
		return processForBeanList(builder.toString());
	}
	
	@Override
	public java.util.List<Bug> fetchOpenDefects() {
		StringBuilder builder = new StringBuilder(URL_PREFIX);
		builder.append("?status=").append(Status.ASSIGNED.getValue());
		builder.append(",").append(Status.NEW.getValue());
		builder.append("&include_fields=_all");
		return processForBeanList(builder.toString());
	}

	private List<Bug> processForBeanList(String url) {
		Lock<String> lock = new Lock<String>();
		HTTPRequestAsync async = new HTTPRequestAsync(url, lock);
		async.execute();

		while (!lock.isDataSet()) {
			// Do Nothing
		}

		if (lock.getData() == null) {
			return null;
		}

		Gson gson = new Gson();
		BugHolder response = gson.fromJson(lock.getData(), BugHolder.class);

		if (response.getBugs() == null) {
			return new ArrayList<Bug>();
		}
		return response.getBugs();
	}

}
